import psutil
import re
from src.core.config.luc import LOL_UX_PATH

"""
客户端,存储端口,token,以及base_url
"""
class Client:
    def __init__(self, port, token, base_url):
        self.port = port
        self.token = token
        self.base_url = base_url

    def get_info(self):
        return self.port, self.token, self.base_url
    
    def get_base_url(self):
        return self.base_url

    def is_valid(self):
        return self.port is not None and self.token is not None
    
"""
获取 LCU API 的认证信息
"""
def get_lcu_auth():
    process_name = LOL_UX_PATH
    token = None
    port = None

    # 遍历所有进程，找到 LeagueClientUx.exe
    for proc in psutil.process_iter(attrs=["name", "cmdline"]):
        if proc.info["name"] == process_name:
            cmdline = " ".join(proc.info["cmdline"])  # 合并为字符串
            # 正则提取 token 和 port
            match = re.search(r'--remoting-auth-token=([^\s"]+).*?--app-port=(\d+)', cmdline)
            if match:
                token = match.group(1)
                port = int(match.group(2))
                break

    return port, token